﻿Imports Runa.Core.GamePackage
Imports Runa.Common.Helper
Imports Runa.Foundation.Diagnostics

' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class GameLogViewerPage
    Inherits Page

    Private m_gamePackage As GamePackageBase

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call. 
    End Sub

    Public Sub New(ByVal g As GamePackageBase)
        InitializeComponent()
        Me.m_gamePackage = g
        Me.Layout.DataContext = Me.m_gamePackage
        For i As Integer = 1990 To 2099
            Me.CBox_Year.Items.Add(i)
        Next
        For i As Integer = 1 To 12
            Me.CBox_Month.Items.Add(i)
        Next
        AddHandler Me.CBox_Year.SelectionChanged,
            Sub(sender, e)
                Call UpdateLog()
            End Sub
        AddHandler Me.CBox_Month.SelectionChanged,
            Sub(sender, e)
                Dim days = 31
                Dim month = CType(CType(sender, ComboBox).SelectedItem, Integer)
                If {1, 3, 5, 7, 8, 10, 12}.Contains(month) Then
                    days = 31
                ElseIf month = 2 Then
                    Dim year = CType(Me.CBox_Year.SelectedItem, Integer)
                    If IsLeap(year) Then
                        days = 28
                    Else
                        days = 29
                    End If
                Else
                    month = 30
                End If
                For i As Integer = 1 To days
                    Me.CBox_Day.Items.Add(i)
                Next
                Call UpdateLog()
            End Sub
        AddHandler Me.CBox_Day.SelectionChanged,
            Sub(sender, e)
                Call UpdateLog()
            End Sub
        Me.CBox_Year.SelectedItem = DateTime.Now.Year
        Me.CBox_Month.SelectedItem = DateTime.Now.Month
        Me.CBox_Day.SelectedItem = DateTime.Now.Day
    End Sub

    ''' <summary>
    ''' Invoked when this page is about to be displayed in a Frame.
    ''' </summary>
    ''' <param name="e">Event data that describes how this page was reached.  The Parameter
    ''' property is typically used to configure the page.</param>
    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)

    End Sub

    Private Sub UpdateLog()
        Try
            Dim year = CType(Me.CBox_Year.SelectedItem, Integer)
            Dim month = CType(Me.CBox_Month.SelectedItem, Integer)
            Dim day = CType(Me.CBox_Day.SelectedItem, Integer)
            Dim logs = (From item In Tracer.Logs(Me.m_gamePackage.Name).Log Where item.Item1 = New Date(year, month, day) Select item.Item2).ToList()
            Me.TB_Log.Text = logs.ToString(Function(obj) obj.ToString(), vbCrLf, "", "")
        Catch ex As Exception
            Me.TB_Log.Text = ""
        End Try
    End Sub

End Class